package com.icetech.eom.controller.tx;


import com.github.pagehelper.PageInfo;
import com.icetech.commonbase.ResultTools;
import com.icetech.commonbase.constants.CodeConstantsEnum;
import com.icetech.commonbase.domain.Page;
import com.icetech.commonbase.domain.response.ObjectResponse;
import com.icetech.eom.common.annotation.SystemLog;
import com.icetech.eom.controller.BaseController;
import com.icetech.eom.controller.vo.SmsSearchVo;
import com.icetech.eom.domain.tx.Smslist;
import com.icetech.eom.service.tx.ISmslistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@RestController
@RequestMapping("/msg")
@Api(value = "Server-API",description = "这是短息接口详细信息的描述")
public class SmslistController extends BaseController {
    @Autowired
    private ISmslistService smslistService;
    /**
     * 获取短信列表信息
     */
    @ApiOperation(value = "获取短信列表信息", notes = "")
    @RequestMapping(value = "/listpage",method = RequestMethod.POST)
    @SystemLog(description = "获取短信列表信息")
    public ObjectResponse<Page> getLogs(@RequestBody SmsSearchVo smsSearchVo){
        //获取服务器列表信息
        PageInfo<Smslist> pageInfo = smslistService.selectByPage(smsSearchVo.getSearchKey(), smsSearchVo.getBeginTime(),
                smsSearchVo.getEndTime(), smsSearchVo.getPage(), smsSearchVo.getPageSize());
        return ResultTools.getPageResponse(pageInfo.getList(),pageInfo.getTotal(),pageInfo.getPages());
    }

    /**
     * 删除短信
     */
    @ApiOperation(value = "短信删除", notes = "删除短信信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "int")
    })
    @SystemLog(description = "短信删除")
    @RequestMapping(value = "/remove",method = RequestMethod.GET)
    public ObjectResponse remove(@RequestParam("id")Long id){
        Smslist smslist = smslistService.getById(id);
        if (Objects.isNull(smslist)){
            return ResultTools.fail(CodeConstantsEnum.ERROR_404.getCode(),CodeConstantsEnum.ERROR_404.getDesc());
        }
        boolean isSuccess = smslistService.removeById(id);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }
    /**
     * 批量删除
     */
    @ApiOperation(value = "短信批量删除", notes = "批量删除短信信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id组成的数组", paramType = "query", dataType = "int")
    })
    @SystemLog(description = "短信批量删除")
    @RequestMapping(value = "/batchremove",method = RequestMethod.GET)
    public ObjectResponse batchRemove(@RequestParam("ids") List<Long> ids){
        boolean isSuccess = smslistService.removeByIds(ids);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }

    /**
     * 导出
     * @param searchKey
     * @param response
     * @return
     */
    @SystemLog(description = "导出短信信息")
    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public ObjectResponse export(@RequestParam(value = "searchKey",defaultValue = "") String searchKey, HttpServletResponse response){
        try {
            Workbook workbook = smslistService.exportExcel(searchKey);
            String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".xls";
            setFileResponse(response, fileName);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
        }
        return ResultTools.success(null);
    }
}
